<?

/*	CalendarList OBJECT
*	Comment here
*
*					*/


class CalendarList extends MOD {

	function __construct( $vars ) {
		global $CAL, $Calnode;

		$this->_addvars( $vars );
		$this->_ref();
		
#		Load Calendar class.
		$CAL = new CALENDAR();

#		If Object is given, load calendar object.
		if( $this->vars->id )
			$Calnode = new CALNODE( $this->vars->type, $this->vars->id );
	
	}
	
	function output () {
	
		if( $this->action )
			$nodes = $this->{ "filter_" . $this->action }();

		$this->addclass( $this->action );
		
		$result = ( is_array( $nodes ))?
			"<ul>" . implode("\n", $nodes ) . "</ul>\n" : (( $nodes )? $nodes : (( $nodes ) ? $nodes : ".Empty" ));
	
		return $this->parse( $result ) . $this->render_js();
			
	}
	
	function filter_simple() {
		global $CAL;
		
		$this->vars->lisort = array( "date", "time", "name" );
		
		if( $nodes = $CAL->filter_nodes( $this->vars->from, (int) $this->vars->diff, "cal.single", $this->vars->objtype, $this->vars->loc ))
			foreach( $nodes as $Node ) {
				
				if( !$day[ $str = $Node->get( 'datestr' )] && $day[ $str ] = true )
					$lines[] = $this->render_li( "", "day", 0, array( "date"=> $Node->get( 'datestr' )));
	
				if( $Node->obj && $Node->obj->active() )
					$lines[] = $this->render_li( $Node->obj->get( 'name' ), 0, $Node->obj->uri(), array( "time"=> $Node->get( 'timestr' )));
			}

		return $lines;
	}
	
	private function filter_upcoming() {
		global $DB, $CAL;
		
		if( $nodes = $CAL->filter_nodes( "now()", (int) $this->vars->diff, $this->vars->type, $this->vars->objtype, $this->vars->loc, $this->vars->limit ))
			foreach( $nodes as $Node )
				
				$lines[] = "<li><a href='{$Node->url()}'>{$Node->obj->get('name')}</a><span class='date'>{$Node->date('d.m.y')}</span></li>";
				
		return "<ul style='list-style-type:none'>" . implode("\n", $lines ) . "</ul>\n";
	}	

	
	private function filter_week() {
		global $DB, $CAL, $Engine, $Cal;
		
		$CAL->dformat = "%d.%m";
		$this->vars->lisort = array( "date","name","content" );
		
		if( $days = $CAL->filter_nodes( date( "Y-m-d", strtotime( "-".((int)date( "w" ) - 1)." days" )), 7, $this->vars->type ))
			foreach( $days as $Day )
				$nodes[] = $this->render_li( $Day->get( "name" ), 0, ($this->vars->uri)? $Day->uri(): 0, array( "date"=>$Day->get("datestr"), "content"=>$Day->get( "content" )));
		
		return $nodes;
		
		/*if( $nodes = $CAL->filter_nodes( $CAL->smartday( $Cal->get( "date" )), 1 )) 
			foreach( $nodes as $node ) {
			
				$text = ( $id = $node->get( 'textid' ))? $Engine->text->get( $id, "long" ) : "";
			
				if( $node->get( "objid" ) ) {
					$content = "<span class='time'>{$node->get( 'timestr' )}</span><a class='name' href='{$node->obj->url()}'>{$node->obj->get( 'name' )}</a><span class='loc'>{$node->loc->get( 'name' )}</span><span class='descr'>$text</span>";
					$lines[] = ( $with_nfo )? "<li class='obj'>$content</li>" : "<li class='none'>$content<span class='cont'>" . substr( $Engine->text->get( $node->obj->get( "textid" ), "long" ), 0, 142 ). " ...</span></li>";
				} else {
					$with_nfo = true;
					$lines[] = "<li class='nfo'><h1>{$node->get( 'name' )}</h1>$text</li>";
				}
				
					
			}
		
		$this->nodes = $nodes;
		return $lines;*/
	}	

	
	/*
	89
	{ "mod":"calendar.basic","class":"stretch", "action":"visual", "vars":{"type":"cal.single","objtype":"event", "imgsize":"124.0", "limit":6}, "nameid":3111 }
	
	118
	{ "mod":"calendar.list","action":"media","class":"black oversize","vars":{ "calref":"cal.single", "mediatype":"media.img", "imgsize":"142.0", "type":"event", "past":"true", "limit": 24 }}
 
 	67
 	{ "mod":"calendar.day", "action":"media","vars":{"type":"media.img","sze":"200.0","href":"true" }}
 
	*/
	
	public function filter_media() {
		global $CAL, $DB, $Object;

		
		$Children = ( $this->vars->calref )? $this->filter_media_calref() : (( $this->vars->fromdate )? $this->filter_media_fromdate : (( $Object )? $CAL->filter_nodes_by_object( "cal.single", $this->vars->type ) : null ));
		
		if( $Children )
			foreach( $Children as $Node )
				if( $Node->obj->hasMedia())
					
					$lines[]	= ( $this->vars->mediatype == "media.img" )? $this->render_media_img( $Node, $name ) : null;


		return $lines;
	}
	
	private function filter_media_calref() {
		global $DB, $CAL, $Object;
		
		$reflist	= array();
		$dates		= $CAL->filter_key_by_object( "cal.single", $Object->type, (int) $Object->id );
		
		if( $dates )
			foreach( $dates as $key => $row )
				
				if( $refs = $DB->reference( "cal.single", (int) $key, $this->vars->calref ))
					$reflist = array_merge( $reflist, $refs );
					
		return $CAL->filter_nodes_by_condt( $this->vars->calref, "active && ID in(" . implode( ",", array_unique( $reflist )) . ") group by objid order by rank desc, date desc limit " . $this->vars->limit );
		
	}
	
	private function filter_media_fromdate() {
	
	}
	
	private function render_media_img( $Node ) {
		
		$name		= $Node->hasName()? $Node->get( "name" ) : $Node->obj->get( "name" );
		return "<li><a href='{$Node->obj->uri()}' title='$name'>{$Node->obj->media->render( $this->vars->mediatype, $this->vars->imgsize )}" . ( $this->vars->showtitle? $name : "" ) . "</a></li>";
	}
	
	
	/*function filter_media() {
		global $CAL, $DB, $Object;
		
		if( $this->vars->from ) 
			$nodes = $CAL->filter_nodes( $this->vars->from, (int) $this->vars->diff, "cal.single", $this->vars->objtype, $this->vars->loc, 100 );
			
		else if( $Object ) {
		
			$nodes = $CAL->filter_nodes_by_object( "cal.single", $Object->type, (int) $Object->id );
			foreach( $nodes as $Node ) $ids[] = $Node->id;
			
			if( $ids = $DB->reference( "cal.single", "parentid in(" .implode( ",", $ids ). ")", "cal.single" ))
				$nodes = $CAL->filter_nodes_by_condt( "cal.single", "ID in(" .implode( ",", $ids ). ") && active" );
		}
		//else if( $Object && $ids = $DB->reference( "cal.single", $Object->id, $this->vars->objtype ))
		//	$nodes = $DB->filter_objects( $this->vars->objtype, "ID in(" .implode( ",", $ids ). ") && active" );
	
	
		if( $nodes )
			foreach( $nodes as $Node ) if( $Node->obj->get( 'rank' ) >= $this->vars->rank && $media = $Node->obj->media->get( $this->vars->mediatype ))
				if( $Node->obj->hasMedia()) $lines[$Node->obj->id] = "<li><a href='{$Node->obj->uri()}'><img src='/public.img?id={$media->ID}&sze={$this->vars->imgsize}' alt=\"{$Node->obj->get('name')}\" /></a></li>";
			
		return $lines;
		
	}*/
	
	
	
	

/*
{ "mod":"calendar.list", "action":"full", "vars":{
	"from"			: "now()",
	"diff"			: 356,
	"objtype"		: "band",
	"imgsize"		: "80.32",
	"date_include"	: "band"
}}


{ "mod":"calendar.list","action":"full","vars":{
	"from":"now()",
	"diff":30,
	"objtype":"band",
	"date_include":"band",
	"imgsize":"78.20&msk=1"
}}


*/

	function filter_object() {
		global $CAL, $Object;
		
		if( $Object->type != $this->vars->type )
			if( $Object->obj && $Object->obj->type == $this->vars->type ) $Object = $Object->obj;
			else return null;
			
		$nodes = $CAL->filter_nodes_by_object( "cal.single", $Object->type, (int) $Object->id, "active order by date desc limit " . $this->vars->limit );
		
		$active = ( count( $nodes ) > 1 && $Active = $CAL->get_node( "now()", "cal.single", $Object->type, (int) $Object->id ))?
					$Active->get( "datestr" ) : $nodes[ 0 ]->get( "datestr" );

		if( $nodes )
			foreach( $nodes as $Node )
				$lines[] = ( $active == $Node->get( "datestr" ))?
					"<li><a href='{$Node->uri()}' class='active'>{$Node->date( "d.m Y", null, true )}</a><span class='content'>" . ( $Node->hasContent()? $Node->get( "content" ) : null ) . "</span></li>" :
					"<li><a href='{$Node->uri()}'>{$Node->get( "datestr" )}</a></li>";
					
		return $lines;
	}
	

	function filter_full() {
		global $CAL, $Object;
		
		if( $this->vars->inherit_loc )
			$this->vars->loc = ( $Object->loc )? $Object->loc->id : $Object->id;

		if( $nodes = $CAL->filter_nodes( $this->vars->from, (int) $this->vars->diff, "cal.single", $this->vars->objtype, $this->vars->loc, 100 ))
		
			foreach( $nodes as $Node ) {
			
				if( !$day[ $str = $Node->smartday()] && $day[ $str ] = true )
					$lines[] = "<li class='sep'><a href='{$Node->uri()}'>{$Node->get( 'datestr' )}</a><span></span><span class='tags'>{$this->render_includes( $Node->get( "date" ), 2 )}</span></li>";
				
				if( $Node->obj ) {
					$img		= $Node->obj->hasMedia()? "<a href='{$Node->obj->uri()}'><img src='/public.img?id={$Node->obj->media->get( 'media.img' )->ID}&sze={$this->vars->imgsize}' alt='{$Node->obj->get( 'name')}' /></a>" : "";
					$descr		= $Node->hasContent()? "<span class='descr'>{$Node->get( "content" )}</span>" : "<span></span>";
					$lines[] 	= "<li><a class='name' href='{$Node->uri()}'>{$Node->obj->get( 'name' )}</a>$descr<span class='tags'>{$this->objTag( $Node->obj )}{$this->objTag( $Node->loc, 'loc', 1 )}$img</span></li>";
				
				} else $lines[] = "<li><i>{$Node->get( 'name' )}</i><span class='descr'>{$Node->get( "content" )}</span></li>";
			}
			
		return $lines;
	}

	
	function filter_diverse() {
		global $CAL;
		
		if( !$sort = $this->vars->lisort )
			$this->vars->lisort = $sort = array( "date", "include", "time", "img", "name", "ext", "loc", "cont" );
		
		if( $nodes = $CAL->filter_nodes( $this->vars->from, (int) $this->vars->diff, "cal.single", $this->vars->objtype, $this->vars->loc, 100 ))
			foreach( $nodes as $Node ) {
				unset( $span );
				//$Node->get( 'datestr' )
				if( !$day[ $str = $Node->smartday()] && $day[ $str ] = true )
					$lines[] = $this->render_li( "", "day", 0, array( "date"=> $Node->get( 'datestr' ), "include"=> $this->render_includes( $Node->get( "date" ))));
				
				if( $this->vars->imgsize && $Node->obj && $id = $Node->obj->media->get( "media.img" )->ID )	
													$span[ "img" ]	= "<img src='/public.img?id=$id&sze={$this->vars->imgsize}' alt='{$Node->obj->get("name")}'>";
				
				else if( in_array( "time", $sort ))	$span[ "time" ]	= $Node->get( 'timestr' );
				if( in_array( "loc", $sort ))	$span[ "loc" ]	= $Node->loc->get( 'name' );
				if( in_array( "ext", $sort ))	$span[ "ext" ]	= ( $ext = $this->vars->ext )? $ext : "<a href='{$Node->obj->uri()}'>{$Node->obj->type}</a>";
				
				
				
				if( in_array( "cont", $sort ) && $Node->hasContent())
					$span[ "cont" ]	= $Node->get( "content" );
				
				if( $Node->obj && $Node->obj->active() )
					$lines[] = $this->render_li( $Node->obj->get( 'name' ), ( $Node->obj->get( "rank" ) > 8 )? "select" : $Node->obj->type, $Node->obj->uri(), $span );

			}

		return $lines;
	}	
	
	
	function filter_alldates() {
		global $CAL, $DB, $Object;
		
		
		if( $days = $CAL->filter_nodes_by_object( "cal.single", $Object->type, (int) $Object->id, "active order by date desc limit " . $this->vars->limit ) ) {
		
			$active = ( count( $days ) > 1 && $Active = $CAL->get_node( "now()", "cal.single", $Object->type, (int) $Object->id ))?
				$Active->get( "datestr" ) : $days[ 0 ]->get( "datestr" );
				
		
			foreach( array_reverse( $days ) as $Day ) {
			
				$datestr = $Day->get( "datestr" );
				$tags[] = "<span" . (( $active == $datestr )? " class='active'": "" ) . ">$datestr</span>";
				
				$this->vars->lisort = ( $is_days = ( $Day->get( "span" ) > 24 * 60 ))? array( "time","name","loc","cont" ) : array( "time","name","cont" );
				if( $list = $this->render_timelist( $Day, !$is_days ))
					$clusters[] = "<ul class='cluster" . (( $active == $datestr )? " active": null ) . "'>" . implode( "\n", $list ) . "</ul>"; 

			}
			if( $tags ) 	$lines[] = "<li class='days'>" . implode( " \n", $tags ) . "</li>";
			if( $clusters )	$lines[] = "<li>" . implode( "</li>\n<li>", $clusters ) . "</li>";
			
			if( count( $days ) > 1 )
				$this->js = "
					mod = new CalendarList( ". json_encode( $this->vars ) .", '{$this->action}' );
					mod.init();
				";
		}
		
		return $lines;
	}
	

	
#	Render html: time-sorted list.
	function render_timelist( $Day, $location ) {
		global $DB, $CAL;
		
		if( $ids = $DB->reference( "cal.single", "parentid = {$Day->id}", "cal.single" ))
			$nodes = $CAL->filter_nodes_by_condt( "cal.single", "ID in(" .implode( ",", $ids ). ") && active order by " . ( $location? "location, date" : "date" ));

		if( $nodes ) foreach( $nodes as $Node ) {
			
			if( !$day[ $str = $Node->loc->id ] && $day[ $str ] = true ) {
				
				$loc = $Node->loc->get( "name" );
				if( $location ) $lines[] = $this->render_li( $loc, "location", $Node->loc->uri(), array( "cont"=> $Node->loc->get( "content" ) ) );
			}
			
			$vars = array( "time" => $Node->get( "timestr" ), "loc"=> $loc, "cont"=> ($Node->get( "textid" ))? $Node->get( "content" ) : "" );
			$lines[] = $this->render_li( $Node->obj->get( "name" ), false, $Node->obj->uri(), $vars );
		}
		
		return ( $lines )? $lines : array( "<li>empty.</li>" );
		
	}
	

#	Render html: list item.
	function render_li( $name, $class = false, $uri = false, $span = array()) {
		
		$span['name'] = ( $uri )? "<a href='$uri'>$name</a>" : $name;
		foreach( $this->vars->lisort as $key )
			if( $span[$key] ) $item .= "<span class='$key'>{$span[$key]}</span>";
		
		return "<li" . (( $class )? " class='$class'" : "" ) . ">$item</li>";
		
	}
	
#	Render html: date row includes.
	function render_includes( $date, $limit = 5 ) {
		global $CAL;
		
		$date = explode( " ", $date );
		
		if( $inc = $this->vars->date_include )
			if( $nodes = $CAL->filter_nodes( $date[ 0 ] . " 06:00:00", 1, "cal.single", $inc, 0, $limit ))
				foreach( $nodes as $Node ) $result[] = "<a href='{$Node->obj->uri()}'>{$Node->obj->get('name')}</a>";
		
		return ( $result )? implode( "", $result ) : "";
	}
	
	private function objTag( $Obj, $type = "bio", $name = false ) {
		
		$name = ( $name )? $Obj->get( 'name' ) : $type;

		return "<a href='{$Obj->uri()}' class='$type'>$name</a>";
	}	
	
	public function api() {
		global $CAL;
				
		$json = array();
		if( $nodes = $CAL->filter_nodes( "now()", (int) $this->vars->diff, $this->vars->type, $this->vars->objtype, $this->vars->loc, $this->vars->limit )) {
			$i = 0;
			foreach( $nodes as $Node ) {
				
				foreach( $this->vars->subkeys as $subkey ) {
					if( $subkey == 'date' )
						$json[ $i ][ $subkey ] = $Node->date( $this->vars->dformat );	
					else if( $subkey == 'img' && !empty( $Node->obj->media->get( 'media.img' )->ID ))
						$json[ $i ][ $subkey ] = engine_get( "url" ) . "/public.img?id={$Node->obj->media->get( 'media.img' )->ID}&sze={$this->vars->imgsize}";		
					else if( $subkey == 'url' )
						$json[ $i ][ $subkey ] = $Node->url();
					else		
						$json[ $i ][ $subkey ] = $Node->obj->get( $subkey );
				}
			$i++;
			}
		}	
		echo json_encode( array( $this->vars->key => $json ));
	}
	
} 

?>


